import datetime
import time

# 将来简化一点代码
def sleep(s):
    # 唯一一个地方的例外
    time.sleep(s)
    return

# 将日期字符串转为时间再比较，time，datetime,str
def compareDateTime(timestr):
    # 获取当前时间日期
    #  Wed Jul 22 15:50:26 2020
    # Friday, November 18, 2016
    nowTime_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    # print(nowTime_str)
    # mktime参数为struc_time,将日期转化为秒，
    e_time = time.mktime(time.strptime(nowTime_str, "%Y-%m-%d %H:%M:%S"))
    # print(e_time)
    try:
        s_time = time.mktime(time.strptime(timestr, '%a %b %d %H:%M:%S %Y'))
        # print(s_time)
        # 日期转化为int比较【now-传入】
        diff = int(e_time) - int(s_time)
        # print(diff)
        # if diff >= 0:
        #     return 1
        # else:
        #     print('传入日期时间比系统当前时间要小')
        #     return 0
        return diff
    except Exception as e:
        return 0

def getWeekOfMonth(year, month, day):
    """
    获取指定的某天是某个月中的第几周
   周一作为一周的开始
   """
    end = int(datetime.datetime(year, month, day).strftime("%W"))
    begin = int(datetime.datetime(year, month, 1).strftime("%W"))
    return end - begin + 1

def getLastWeekOfYear(year, month, day):
    """
    获取指定的某天是某年中的第几周，自然周
   """
    # str(datetime.date(year, month, day).isocalendar()[1])    本周
    # str(datetime.date(year, month, day).isocalendar()[1] - 1)    上周
    return str(datetime.date(year, month, day).isocalendar()[1] - 1)

def getWeekOfYear(year, month, day):
    """
    获取指定的某天是某年中的第几周，自然周
   """
    # str(datetime.date(year, month, day).isocalendar()[1])    本周
    # str(datetime.date(year, month, day).isocalendar()[1] - 1)    上周
    return str(datetime.date(year, month, day).isocalendar()[1])


if __name__ == '__main__':
    print(compareDateTime("Thu Jul 23 08:56:00 2020"))
    print("====================================")
    print(getLastWeekOfYear(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day))
